import Vue from 'vue';
import request from '@/utils/request';
function setImgSrc(el, binding) {
    if (binding.oldValue === undefined || binding.value !== binding.oldValue) {
        const imageUrl = binding.value;
        request({
            method: 'get',
            url: imageUrl,
            responseType: 'arraybuffer'
        })
            .then(function(resp) {
                const mimeType = resp.headers['content-type'].toLowerCase();
                const imgBase64 = Buffer.from(resp.data, 'binary').toString('base64');
                el.src = 'data:' + mimeType + ';base64,' + imgBase64;
            })
            .catch(function() {
                el.src = imageUrl;
            });
    }
}
Vue.directive('auth-src', {
    bind: function(el, binding) {
        setImgSrc(el, binding);
    },
    componentUpdated: function(el, binding) {
        setImgSrc(el, binding);
    }
});
